
import React, { Component } from 'react'
import { Switch, Route, Redirect } from 'react-router-dom'
class RouterView extends Component {
  static defaultProps = {
    routes: [],
    matchPath: ''
  }
  render() {
    const { routes, matchPath } = this.props
    return (
      <Switch>
        {
          routes?.map((item, index) => {
            //重定向
            if (item.from) {
              return <Redirect exact key={-1} from={matchPath + item.from} to={item.to}></Redirect>
            }
            //路由
            const Com = item.component;
            return <Route key={index} path={matchPath + item.path} render={(props) => <Com {...props} routes={item.children}></Com>}></Route>
          })
        }
      </Switch>
    )
  }
}

export default RouterView